package com.erbao.io.netty.refactor.transport;

import com.erbao.io.netty.refactor.rpc.Dispatcher;
import com.erbao.io.netty.refactor.rpc.protocol.MyContent;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author CaoBaoYin
 * @since 23/9/5 18:04
 */
public class MyHttpServerServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletInputStream is = req.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        try {
            MyContent reqContent = (MyContent) ois.readObject();
            Object result = Dispatcher.dispatcher.getResult(reqContent);

            MyContent respContent = new MyContent();
            respContent.setResponseObj(result);

            ServletOutputStream os = resp.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(respContent);
            oos.flush();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }


        super.doPost(req, resp);
    }
}
